//
//  MyGoogleWeather.m
//  Ghac 4S
//
//  Created by apple on 11-12-9.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyGoogleWeather.h"
#import "KURL.h"
#import "KXML.h"
#import "KXMLNode.h"

@interface MyGoogleWeather(private) 
-(void)askCityWeatherWithName:(NSString *)cityName;
-(void)updateLocationComplete:(id)sender;
-(void)updateWeatherComplete:(id)sender;
@end

@implementation MyGoogleWeather
-(void)updateLocationCoordinate:(CLLocationCoordinate2D)coordinate
{
    NSString* str = [NSString stringWithFormat:@"%@q=%f,%f&gflags=XR&locale=zh-cn", WEATHER_CITY_QUERYPATH, coordinate.latitude, coordinate.longitude];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL* cityinfourl = [NSURL URLWithString:str];
    KURLLoader* loader = [[KURLLoader alloc] initWithDelegate:self];
    KURLRequest* req = [[KURLRequest alloc] initWithURL:cityinfourl];
    [loader load:req];
    [loader addEventListener:KLOADER_COMPLETE sector:@selector(updateLocationComplete:)];
    [req release];
    
    
}
-(void)updateLocationComplete:(id)sender
{
    KURLLoader* loader = sender;
    NSStringEncoding encode = NSUTF8StringEncoding;
    NSString* xmlStr = [[NSString alloc] initWithData:loader.data encoding:encode];
    if (xmlStr == nil)
    {
        return;
    }
    
    
    KXML* cityinfo = [[KXML alloc] initWithString:xmlStr];
    [xmlStr release];
    [loader release];
    
    if ([[cityinfo valueForKeyPath:@"Error.__nodeValue__"] isEqualToString:@"0"])
    {
        [self askCityWeatherWithName:[cityinfo valueForKeyPath:@"Result.city.__nodeValue__"]];
    }
    else
    {
        NSError* err = [NSError errorWithDomain:@"error" code:0 userInfo:[NSDictionary dictionaryWithObject:[cityinfo valueForKeyPath:@"ErrorMessage.__nodeValue__"] forKey:@"ErrorMessage"]];
        [self errorOnGettingCity:err];
    }
    [cityinfo release];
}
-(void)askCityWeatherWithName:(NSString *)cityName
{
    NSString* str = [NSString stringWithFormat:@"%@/ig/api?hl=zh-cn&weather=%@", WEATHER_QUERYPATH, cityName];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL* weatherinfourl = [NSURL URLWithString:str];
    
    KURLLoader* loader = [[KURLLoader alloc] initWithDelegate:self];
    KURLRequest* req = [[KURLRequest alloc] initWithURL:weatherinfourl];
    [loader load:req];
    [loader addEventListener:KLOADER_COMPLETE sector:@selector(updateWeatherComplete:)];
    [req release];
    
}
-(void)updateWeatherComplete:(id)sender
{
    KURLLoader* loader = sender;
    NSStringEncoding encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    
    NSString* xmlStr = [[NSString alloc] initWithData:loader.data encoding:encode];
     NSLog(@"load weather completed %@", xmlStr);
    if (xmlStr == nil)
    {
        return;
    }
    
    KXML* weatherinfo = [[KXML alloc] initWithString:xmlStr];
    KXMLNode* conditionNode = [weatherinfo valueForKeyPath:@"weather.forecast_conditions"];
    self.templeture =[NSString stringWithFormat:@"%@,%@,%@~%@", [weatherinfo valueForKeyPath:@"weather.forecast_information.postal_code.@data"], [conditionNode valueForKeyPath:@"condition.@data"],[conditionNode valueForKeyPath:@"low.@data"], [conditionNode valueForKeyPath:@"high.@data"]] ;
    
    NSString* imageStr =[NSString stringWithFormat:@"%@%@", WEATHER_QUERYPATH, [conditionNode valueForKeyPath:@"icon.@data"]];
    self.weatherImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageStr]]];
    [m_delegate onWeatherUpdate:self];
    //    NSLog(@"%@", weatherinfo);
    [weatherinfo release];
    [xmlStr release];
    [loader release];
}
@end

